package com.hjc.hjcojsandbox;




import cn.hutool.json.JSONUtil;
import java.util.*;
public class SimpleCompute {
        //输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
        public static void main(String[] args) {
            String numsStr = args[0];
            String targetStr = args[1];
            // 步骤1：去除首尾的 []
            String trimmed = numsStr.substring(1, numsStr.length() - 1);
            // 结果：" 1, 2, 3 "
            // 步骤2：去除所有空格\
            String noSpaceStr = trimmed.replaceAll(" ", "");
            // 结果："1,2,3"
            // 步骤3：按逗号分割
            String[] strElements = noSpaceStr.split(","); // 结果：["1", "2", "3"]
            // 步骤4：转换为 int 数组
            int[] nums = new int[strElements.length];
            for (int i = 0; i < strElements.length; i++) {
                nums[i] = Integer.parseInt(strElements[i]);
            }
            int k = Integer.parseInt(targetStr);
            Solution solution = new Solution();
           int [] ans = solution.maxSlidingWindow(nums,k);
            System.out.println(JSONUtil.toJsonStr(ans));

        }
    }

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        Deque<Integer> deque = new LinkedList<>();
        for(int i =0;i<k;i++){
            while(!deque.isEmpty()&&nums[i]>nums[deque.peekLast()]){
                deque.pollLast();
            }
            deque.offerLast(i);
        }
        int [] ans = new int[n-k+1];
        ans[0] = nums[deque.peekFirst()];
        for(int i = k;i<n;i++){
            while(!deque.isEmpty()&&nums[i]>nums[deque.peekLast()]){
                deque.pollLast();
            }
            deque.offerLast(i);
            while (deque.peekFirst() <= i - k) {
                deque.pollFirst();
            }
            ans[i-k+1] = nums[deque.peekFirst()];
        }
        return  ans;
    }
}